package com.qf.springbootproject.index.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.springbootproject.index.service.ProductService;
import com.qf.springbootproject.index.entity.Product;
import com.qf.springbootproject.index.entity.ProductImg;
import com.qf.springbootproject.index.mapper.ProductMapper;
import com.qf.springbootproject.index.service.ProductImgService;

import com.qf.springbootproject.product.entity.ProductSku;
import com.qf.springbootproject.product.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author yyq
 * @since 2023-02-14
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    ProductImgService productImgService;
    @Autowired
    ProductSkuService productSkuService;

    /**
     * 新品推荐
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Product> productImg() {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.orderByDesc("create_time").last("limit 0,3");
        List<Product> records = list(productQueryWrapper);
        for (Product record : records) {
            QueryWrapper<ProductImg> productImgQueryWrapper = new QueryWrapper<>();
            String productId = record.getProductId();
            productImgQueryWrapper.eq("item_id", productId);
            List<ProductImg> list = productImgService.list(productImgQueryWrapper);
            record.setImgs(list);
        }
        return records;
    }

    /**
     * 根据商品id查询商品基本信息优化版
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Map<String, Object> detailInfo(Integer id) {
        //查询商品信息
        Product product = getById(id);
        //查询商品套餐信息
        List<ProductSku> skuList = productSkuService.list(new QueryWrapper<ProductSku>().eq("product_id", id));
        //查询商品图片信息
        List<ProductImg> imgList = productImgService.list(new QueryWrapper<ProductImg>().eq("item_id", id));
        HashMap<String, Object> map = new HashMap<>();
        map.put("product", product);
        map.put("productSkus", skuList);
        map.put("productImgs", imgList);
        return map;
    }
}












